package com.xingchi.tornado.mybatisplus.config;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xingchi.tornado.unique.provider.impl.SnowflakeProvider;
import org.springframework.stereotype.Component;

/**
 * MyBatis-Plus自定义ID生成器
 * 基于雪花算法实现自增ID生成
 *
 * @author xingchi
 * @date 2024/5/28 13:51
 */
@Component
public class CustomIdGenerator implements IdentifierGenerator {

    private final SnowflakeProvider snowflakeProvider;

    /**
     * 构造函数注入依赖
     *
     * @param snowflakeProvider 雪花算法ID生成器
     */
    public CustomIdGenerator(SnowflakeProvider snowflakeProvider) {
        this.snowflakeProvider = snowflakeProvider;
    }

    /**
     * 生成数值类型的ID
     *
     * @param entity 实体对象
     * @return 雪花算法生成的ID
     */
    @Override
    public Number nextId(Object entity) {
        return snowflakeProvider.nextId();
    }
}
